from appmanger.lib.dbconfig import api_db
from django.http import JsonResponse
from appmanger.lib.getlog import get_log
import ast
import json
logger = get_log()
from builtins import str


# API信息查询
def api_info(request):
    response = {}
    try:
        body = ast.literal_eval(str(request.body, encoding="utf-8"))
        module_name = body['module_name']
        api_suggest = body['api_suggest']
        currentPage = body['currentPage']
        pagesize = body['pagesize']
        start = (currentPage - 1) * pagesize
        limit_sql = " limit %s,%s" % (start, pagesize)
        if module_name == '' or module_name == None:
            module_sql = ""
        else:
            module_sql = " and module_name like '%%%s%%'" % module_name
        if api_suggest == '' or api_suggest == None:
            suggest_sql = ""
        else:
            suggest_sql = " and api_suggest like '%%%s%%'" % api_suggest
        count_sql = "SELECT count(*) as total FROM api_info WHERE 1=1" + module_sql + suggest_sql
        api_info_sql = "SELECT id,module_name,api_name,api_suggest,api_method,api_url,api_data,api_header FROM api_info WHERE 1=1" + module_sql + suggest_sql + limit_sql
        api_result = api_db(api_info_sql)
        total = api_db(count_sql)
        response['total'] = total
        response['message'] = 'success'
        response['code'] = 200
        response['datas'] = api_result
        logger.info("api信息列表查询成功，响应参数为：%s" % response)
    except Exception as e:
        response['message'] = str(e)
        response['code'] = 500
        logger.error("api信息列表查询异常，异常信息为：%s" % e)
    return JsonResponse(response)


def api_update(request):
    # 修改api接口信息
    response = {}
    try:
        body = ast.literal_eval(str(request.body, encoding="utf-8"))
        api_id = body['id']
        module_name = body['module_name']
        api_name = body['api_name']
        api_suggest = body['api_suggest']
        api_method = body['api_method']
        api_url = body['api_url']
        update_sql = "UPDATE api_info SET module_name='%s',api_name='%s',api_suggest='%s',api_method='%s',api_url='%s' WHERE id='%s'" % (
        module_name, api_name, api_suggest, api_method, api_url, api_id)
        sql_result = api_db(update_sql)
        response['message'] = 'success'
        response['code'] = 200
        response['datas'] = sql_result
        logger.info("api信息修改成功，响应参数为：%s" % response)
    except Exception as e:
        response['message'] = str(e)
        response['code'] = 500
        logger.error("api信息修改异常，异常信息为：%s" % e)
    return JsonResponse(response)


def apidata_query(request):
    # API请求数据查询
    response = {}
    try:
        body = ast.literal_eval(str(request.body, encoding="utf-8"))
        api_id = body['id']
        data_sql = "SELECT api_data FROM api_info WHERE id='%s'" % api_id
        data_result = api_db(data_sql)
        data_info = ast.literal_eval(data_result[0]['api_data'])
        response['message'] = 'success'
        response['code'] = 200
        response['datas'] = data_info
        logger.info("api数据查询成功，响应参数为：%s" % response)
    except Exception as e:
        response['message'] = str(e)
        response['code'] = 500
        logger.error("api数据查询异常，异常信息为：%s" % e)
    return JsonResponse(response)


def apidata_update(request):
    # 修改api数据内容
    response = {}
    try:
        body = ast.literal_eval(str(request.body, encoding="utf-8"))
        api_id=body['id']
        api_data=body['api_data']
        data_str=str(api_data).replace("'",'"')
        update_sql="UPDATE api_info SET api_data='%s' WHERE id='%s'"%(data_str,api_id)
        str_sql=update_sql
        update_result=api_db(update_sql)
        response['message'] = 'success'
        response['code'] = 200
        response['datas'] = update_result
        logger.info("api数据修改成功，响应参数为：%s" % response)
    except Exception as e:
        response['message'] = str(e)
        response['code'] = 500
        logger.error("api数据修改异常，异常信息为：%s" % e)
    return JsonResponse(response)
